/**
 * Created by zjy on 16-2-25.
 */
var fs = require('fs');
var mongoose = require('mongoose');

var c = JSON.parse(process.env.mongodb);

var port = c.port;
var dbName = c.db;
var dbHost = c.host;
var opts= c.opts;

console.log('db connect start:');

var bdUrl = "mongodb://" + dbHost + ":"+ port + "/" + dbName;

mongoose.connect(bdUrl, opts);

// db = db.useDb(dbName);

mongoose.connection.once('open', function () {
    console.log('mongodb connect success');
});

mongoose.connection.on('error', function (err) {
    console.log('--------------------------- mongodb connect error ---------------------------');
    console.warn(err);
});

process.on('SIGINT', function() {
	mongoose.connection.close(function () {
		console.log('--------------------------- mongodb connection closed ---------------------------');
        process.exit(0);
	});
});

exports.db = mongoose;